const path = require("path");
const { mimetype } = require("../config/uploadfile.config");
const {
  createGoods,
  updateById,
  removeGoods,
  restoreGoods,
  findGoods,
} = require("../server/goods.server");

const {
  fileUploadError,
  fileIsNotEmpty,
  unSupportedMimeType,
  publishGoodError,
  updataGoodsError,
  invalidGoodsId,
  deleteGoodsError,
  restoreGoodsError,
  GetGoodsAllError,
} = require("../constants/err.type");

class GoodsController {
  // 上传商品图片
  async upload(ctx, next) {
    const { file } = ctx.request.files;
    console.log(ctx.request.files);
    console.log("ctx.request.files");
    if (!file) {
      return (ctx.body = unSupportedMimeType);
    } else {
      ctx.body = {
        code: 0,
        message: "商品文件上传成功",
        result: {
          goods_img: path.basename(file.filepath),
        },
      };
    }
  }

  // 发布商品
  async create(ctx, next) {
    // 写入商品
    try {
      const res = await createGoods(ctx.request.body);
      const { createdAt, updatedAt, ...result } = res;
      ctx.body = {
        code: 0,
        message: "发布商品成功",
        result: result,
      };
    } catch (error) {
      console.error(error);
      return ctx.app.emit("error", publishGoodError, ctx);
    }
  }

  // 修改商品
  async update(ctx, next) {
    ctx.body = "修改商品成功";
    console.log("request", ctx.request.body);

    const id = ctx.params.id;
    console.log({ id, ...ctx.request.body });
    try {
      const res = await updateById({ id, ...ctx.request.body });
      console.log("修改结果", res);
      if (res) {
      } else {
        return ctx.app.emit("error", invalidGoodsId, ctx);
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", updataGoodsError, ctx);
    }
  }

  // 下架商品
  async remove(ctx, next) {
    try {
      const res = await removeGoods(ctx.params.id);
      /* if(res){
        ctx.body = "删除商品成功"
      }else{
        ctx.body = "删除商品失败"
      } */

      if (res) {
        ctx.body = {
          code: 0,
          message: "下架商品成功",
          result: "",
        };
      } else {
        ctx.app.emit("error", invalidGoodsId, ctx);
      }
    } catch (error) {
      console.error(error);
      ctx.app.emit("error", deleteGoodsError, ctx);
    }
  }

  //上架商品
  async restore(ctx, next) {
    try {
      const res = await restoreGoods(ctx.params.id);
      /* if(res){
          ctx.body = "删除商品成功"
        }else{
          ctx.body = "删除商品失败"
        } */

      if (res) {
        ctx.body = {
          code: 0,
          message: "上架商品成功",
          result: "",
        };
      } else {
        ctx.app.emit("error", invalidGoodsId, ctx);
      }
    } catch (error) {
      console.error(error);
      ctx.app.emit("error", restoreGoodsError, ctx);
    }
  }

  // 商品列表
  async findAll(ctx) {
    const { pageNum = 1, pageSize = 10 } = ctx.request.query;
    try {
      const res = await findGoods(pageNum, pageSize);
      console.log(res);
      ctx.body = {
        code: 0,
        message: "商品获取成功",
        result: res,
      };
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", GetGoodsAllError, ctx);
    }
  }
}

module.exports = new GoodsController();
